package com.xdjj.market.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xdjj.market.dao.GoodsMapper;
import com.xdjj.market.pojo.Goods;
import com.xdjj.market.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    private GoodsMapper goodsMapper;

    @Override
    public Page findGoodsList(Goods searchData,int currentPage,int pageSize) {
        //分页条件对象:参数1：当前的页码，参数2：每页显示的最大记录数
        Page pageRequest =Page.of(currentPage,pageSize);

        //业务的条件对象
        QueryWrapper<Goods> qw =new QueryWrapper<>();

        //一定要判断业务条件是否存在，否则语句会有问题
        //判断是避免空指针异常
        if(searchData!=null){
            if(StringUtils.hasText(searchData.getProductName())){
                qw.like("product_name",searchData.getProductName());
            }
            if(StringUtils.hasText(searchData.getSupplier())){
                qw.eq("supplier",searchData.getSupplier());
            }
            if(StringUtils.hasText(searchData.getPhone())){
                qw.eq("phone",searchData.getPhone());
            }
        }

        //参数1：请求的分页条件对象
        //参数2：业务的条件对象
        Page pageResponse = goodsMapper.selectPage(pageRequest, qw);

        return pageResponse;
    }

//    @Override
//    public List<Goods> findGoodsList() {
//        return goodsMapper.selectList(null);
//    }

    @Override
    public void saveGoods(Goods goods) {
        goodsMapper.insert(goods);
    }

    @Override
    public void updateGoods(Goods goods) {
        goodsMapper.updateById(goods);
    }

    @Override
    public void deleteGoodsById(Integer id) {
        goodsMapper.deleteById(id);
    }
}
